package com.ihr360.payroll.controller.attendanceReport;

import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.lang.PageUtil;
import com.ihr360.commons.vo.PageData;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.model.dto.attendance.report.AttendanceMonthlyReport;
import com.ihr360.payroll.model.response.AttendanceMonthlyResponse;
import com.ihr360.payroll.request.attendance.AttendanceMonthlyRequest;
import com.ihr360.payroll.service.attendancereport.AttendanceBillService;
import com.ihr360.payroll.service.attendancereport.AttendanceMonthlyReportService;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Eric on 2018/3/23/023.
 */
@Api("考勤月报")
@Controller
@RequestMapping(value = "/attendanceMonthlyReport")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.SALARY_ATTENDANCE_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.SALARY_ATTENDANCE_NAME,
        hasExtendsAbstractRestController = true)
public class AttendanceMonthlyReportController extends AbstractRestSimpleWithLongIdController<AttendanceMonthlyReport> {

    @Autowired
    private AttendanceMonthlyReportService attendanceMonthlyReportService;
    @Autowired
    private AttendanceBillService attendanceBillService;

    @Override
    public SimpleRestService<AttendanceMonthlyReport, Long> getSimpleRestService() {
        return attendanceMonthlyReportService;
    }

    @FunctionDefinition(functionName = "获取考勤月报列表", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "获取考勤月报列表")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AttendanceMonthlyResponse monthlyList(AuthUserInfo userInfo, @RequestParam int page, @RequestParam int size, @RequestParam Long summaryId, @RequestParam Boolean isAll) {
        AttendanceMonthlyResponse response = new AttendanceMonthlyResponse();
        Pageable pageable = PageUtil.getPageable(page, size);
        PageData<AttendanceMonthlyReport> pageData = attendanceMonthlyReportService.getMonthlyReportDetail(userInfo.getCompanyId(), pageable, summaryId, isAll);
        response.setPageData(pageData);
        List<Long> reportIdList = pageData.getContent().stream().map(AttendanceMonthlyReport::getId).collect(Collectors.toList());
        response.setListMap(attendanceBillService.getBillTypeMap(userInfo.getCompanyId(), reportIdList));
        return response;
    }

    @FunctionDefinition(functionName = "查询员工考勤月报", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "查询员工考勤月报")
    @PostMapping(value = "/query/one")
    public AttendanceMonthlyReport queryOne(AuthUserInfo userInfo, @RequestBody AttendanceMonthlyRequest request) {
        return attendanceMonthlyReportService.queryOne(userInfo.getCompanyId(), request.getSummaryId(), request.getStaffId());
    }

    @FunctionDefinition(functionName = "保存员工考勤月报", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "/save/one", notes = "保存员工考勤月报")
    @PostMapping(value = "/save/one")
    public Boolean saveAttendanceMonthlyReport(@RequestParam Long summaryId, @RequestBody AttendanceMonthlyReport attendanceMonthlyReport) {
        this.attendanceMonthlyReportService.saveOne(summaryId, attendanceMonthlyReport);
        return true;
    }
}
